﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using HYCodeBase.Math;

namespace YArchitech.Plumbing
{
	public static class XYZUtilityMethod
	{
		public static int PointInPloygon(List<Curve> lstCurves, XYZ pt)
		{
			List<XYZ> list = new List<XYZ>();
			foreach (Curve curve in lstCurves)
			{
				XYZ endPoint = curve.GetEndPoint(0);
				XYZ endPoint2 = curve.GetEndPoint(1);
				list.Add(endPoint);
				list.Add(endPoint2);
			}
			list = list.Distinct<XYZ>().ToList<XYZ>();
			return XYZUtilityMethod.PointInPloygon(list, pt);
		}

		public static int PointInPloygon(List<XYZ> lstCurves, XYZ pt)
		{
			List<XYZ> range = lstCurves.GetRange(0, lstCurves.Count);
			if (range.Count < 3)
			{
				return -1;
			}
			XYZ xyz = range[0];
			XYZ xyz2 = range[range.Count - 1];
			if (xyz != xyz2)
			{
				range.Add(xyz);
			}
			int num = 0;
			List<XYZ> list = new List<XYZ>();
			XYZ xyz3 = new XYZ(0.0, 0.0, 0.0) - pt;
			for (int i = 0; i < range.Count; i++)
			{
				XYZ xyz4 = range[i];
				xyz4 += xyz3;
				list.Add(xyz4);
			}
			XYZ xyz5 = pt + xyz3;
			int num2 = MathUtility.Greaterthan_Or_Equal(list[0].X, 0.0) ? (MathUtility.Greaterthan_Or_Equal(list[0].Y, 0.0) ? 0 : 3) : (MathUtility.Greaterthan_Or_Equal(list[0].Y, 0.0) ? 1 : 2);
			for (int i = 1; i < list.Count; i++)
			{
				XYZ xyz6 = list[i - 1];
				XYZ xyz7 = list[i];
				if (MathUtility.IsEqual(xyz5.DistanceTo(xyz6), 0.0, 0.0001))
				{
					return 0;
				}
				XYZ xyz8 = new XYZ(xyz6.X, xyz6.Y, xyz6.Z);
				XYZ xyz9 = new XYZ(xyz7.X, xyz7.Y, xyz7.Z);
				xyz8 = xyz8.Normalize();
				xyz9 = xyz9.Normalize();
				double num3 = xyz9.Y * xyz8.X - xyz9.X * xyz8.Y;
				if (Math.Abs(num3) < 0.0001 && xyz8.X * xyz9.X <= 0.0001 && xyz8.Y * xyz9.Y <= 0.0001)
				{
					return 0;
				}
				int num4 = MathUtility.Greaterthan_Or_Equal(xyz7.X, 0.0) ? (MathUtility.Greaterthan_Or_Equal(xyz7.Y, 0.0) ? 0 : 3) : (MathUtility.Greaterthan_Or_Equal(xyz7.Y, 0.0) ? 1 : 2);
				if (num4 == (num2 + 1) % 4)
				{
					num++;
				}
				else if (num4 == (num2 + 2) % 4)
				{
					if (num3 > 0.0)
					{
						num += 2;
					}
					else
					{
						num -= 2;
					}
				}
				else if (num4 == (num2 + 3) % 4)
				{
					num--;
				}
				num2 = num4;
			}
			if (num == 0)
			{
				return -1;
			}
			if (Math.Abs(num) == 4)
			{
				return 1;
			}
			return 0;
		}

		public const double _epsPoint = 0.0001;
	}
}
